﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Uipath_License.Model;
using Uipath_License.Tools;

namespace Uipath_License.Uc
{
    /// <summary>
    /// UC_GetLicense.xaml 的交互逻辑
    /// </summary>
    public partial class UC_GetLicense : UserControl
    {
        public UC_GetLicense()
        {
            InitializeComponent();
        }

        private void Get_License_Button_Click(object sender, RoutedEventArgs e)
        {
            

            Thread thread = new Thread(() => 
            {
                Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    //this.Image_GET.Visibility = Visibility.Visible;
                    this.License_Mian_Grid.Visibility = Visibility.Hidden;
                    Get_License_Button.IsEnabled = false;
                    Load_ding.Visibility = Visibility.Visible;
                    Load_ding.IsRunning = true;
                }));
                string url = "https://licensing01api.azurewebsites.net/api/Customer/RegisterCustomerAsync";
                Request_License request_License = new Request_License();
                request_License.Request_License_Default();
                string response_str;
                try
                {
                    response_str = HttpHelper.PostUrlString(url, JsonConvert.SerializeObject(request_License));
                }
                catch (Exception ex)
                {
                    response_str = ex.Message;
                }
                Response_License response_License = JsonConvert.DeserializeObject<Response_License>(response_str);
                Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    //this.Image_GET.Visibility = Visibility.Hidden;
                    this.License_Mian_Grid.Visibility = Visibility.Visible;
                    Get_License_Button.IsEnabled = true;
                    Load_ding.IsRunning = false;
                    Load_ding.Visibility= Visibility.Hidden;
                    LicenseCode_Text.Text = response_License?.LicenseCode;
                    DownloadLink_Text.Text = response_License?.DownloadLink;
                    InstallInstructionLink_Text.Text = response_License?.InstallInstructionLink;

                }));
            });
            thread.Start();

            

            
            
        }
    }
}
